package com.softeem.servlet;

import com.softeem.action.BaseServlet;
import com.softeem.entity.Customer;
import com.softeem.service.CustomerService;
import com.softeem.service.impl.CustomerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


@WebServlet("/customerServlet/*")
public class CustomerServlet extends BaseServlet {

    private CustomerService service = new CustomerServiceImpl();

    public void update() throws Exception {

        Customer cus = new Customer();
        Map<String,String[]> parameterMap = req.getParameterMap();

        BeanUtils.populate(cus,parameterMap);

        Integer i = service.update(cus);

        req.getSession().setAttribute("list",findAll());
        resp.sendRedirect("/list.jsp");
    }

    public void findOne() throws SQLException {

        String id = req.getParameter("id");
        Customer cus = service.findOne(id);

    }

    public void findPages() throws Exception {
        //获取页码
        String pageNo = req.getParameter("pageNo");
        //分页
        List<Customer> list = service.findPages(pageNo);
        //更新session
        req.getSession().setAttribute("list",list);
        //跳转页面
        resp.sendRedirect("/list.jsp");

    }


    public void delete() throws Exception {
        //根据id完成删除
        String id = req.getParameter("id");
        Integer i = service.delete(id);

        if(i==1){
            //更新session request重新运行findAll
            req.getSession().setAttribute("list",findAll());
            resp.sendRedirect("/list.jsp");
        }
    }



    public List<Customer> findAll() throws Exception {
        //读取总页数
        Integer total = service.total();
        //将总页数放入session
        req.getSession().setAttribute("total",total);

        return service.findAll();
    }






    public void login() throws Exception {

        String name = req.getParameter("name");
        String id   = req.getParameter("id");
        String sign = req.getParameter("sign");

        Integer i = service.login(name,id);

        //1.用户名 密码 正确 勾选
        //2.用户名 密码 正确 没有勾选
        //3.用户名 密码 不正确

       if(i==1&&"on".equals(sign)){
           //发cookie
           getCookie(name,id);
           req.getSession().setAttribute("list",findAll());
           resp.sendRedirect("/list.jsp");
       }else if(i==1){
           req.getSession().setAttribute("list",findAll());
           resp.sendRedirect("/list.jsp");
       }else{
           resp.sendRedirect("/login.jsp?status=0");
       }
    }


    public void getCookie(String name,String id) {
        //设置cookie
        Cookie c1 = new Cookie("name",name);
        Cookie c2 = new Cookie("id",id);

        //设置存活时间
        c1.setMaxAge(60*2);
        c2.setMaxAge(60*2);

        //设置访问路径
        c1.setPath("/");
        c2.setPath("/");

        //发送cookie到客户端
        resp.addCookie(c1);
        resp.addCookie(c2);

    }

}
